package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.dao.PermissionDao;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.dao.UserDao;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.User;
import com.itheima.health.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Set;

/**
 * @Author likepei
 * @Date 2020/3/26 2:51
 * @Version v1.0
 * @Description 用户业务接口实现类
 */
@Service
public class UserServiceImpl implements UserService {

    //依赖注入
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;

    /**
     * 基于用户名 获取User对象
     * @param username
     * @return
     */
    @Override
    public User findByUsername(String username) {
        //通过 用户名获取用户信息
        User user = userDao.findByUsername(username);
        //通过 用户名id  获取所关联的角色集合
        Set<Role> roleSet = roleDao.findByUserId(user.getId());
        user.setRoles(roleSet);
        //通过 每一个角色id  获取所关联的权限集合
        for (Role role : roleSet) {
            Set<Permission> permissionSet = permissionDao.findByRoleId(role.getId());
            role.setPermissions(permissionSet);
        }

        return user;
    }

    @Override
    public boolean login(String username, String password) {
        System.out.println("service_provide=======u:" + username + " p:" + password);
        if ("admin".equals(username) && "123".equals(password)){
            return true;
        }
        return false;
    }
}
